package cn.wqk.rrtf.controller;

import cn.wqk.rrtf.pojo.Article;
import cn.wqk.rrtf.pojo.Comment;
import cn.wqk.rrtf.pojo.Teacher;
import cn.wqk.rrtf.pojo.User;
import cn.wqk.rrtf.service.ArticleService;
import cn.wqk.rrtf.service.CommentService;
import cn.wqk.rrtf.service.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

@Controller
public class ArticleController {
    @Autowired
    ArticleService articleService;
    @Autowired
    TeacherService teacherService;
    @Autowired
    CommentService commentService;
    @RequestMapping("/tfPerson")
    public String toTfPerson(Model model){
        List<Article> listeningList=articleService.searchArticleByType("听力");
        List<Article> speakingList=articleService.searchArticleByType("口语");
        List<Article> readingList=articleService.searchArticleByType("阅读");
        List<Article> WritingList=articleService.searchArticleByType("写作");
        model.addAttribute("listeningList",listeningList);
        model.addAttribute("speakingList",speakingList);
        model.addAttribute("readingList",readingList);
        model.addAttribute("WritingList",WritingList);
        return "tfPerson/personMainPage";
    }

    @RequestMapping("/tfPersonDetail")
    public String tfTfPersonDetail(Integer articleId,Model model){
        Article article=articleService.searchArticleByAId(articleId);
        Teacher teacher=new Teacher();
        teacher.setUserId(article.getUserId());
        if(teacherService.searchTeacher(teacher)==null){
            teacher.setName("暂无来源");
        }else{
            teacher=teacherService.searchTeacher(teacher);
        }
        List<Comment> commentList=new ArrayList<Comment>();
        commentList=commentService.searchCommentByAId(articleId);
        model.addAttribute("article",article);
        model.addAttribute("teacher",teacher);
        model.addAttribute("commentList",commentList);
        return "tfPer son/personDetailPage";
    }

    @RequestMapping("/tfPersonAllArticle")
    public String tfPersonAllArticle(Model model){
        List<Article> articleList=new ArrayList<Article>();
        articleList=articleService.searchAllArticle();
        model.addAttribute("articleList",articleList);
        return "tfPerson/personSortPage";
    }

    @RequestMapping("/tfPersonSubmitComm")
    public String tfPersonSubmitComm(Integer articleId,String comm, HttpSession session){
        Comment comment1=new Comment();
        if(session.getAttribute("user")!=null){
            User user=new User();
            user=(User)session.getAttribute("user");
            comment1.setUserId(user.getUserId());  //填写session中的用户Id
            comment1.setArticleId(articleId);
            comment1.setContent(comm);
            if(commentService.addComment(comment1)!=0) {
                return "redirect:/tfPersonDetail"+"?articleId="+articleId; //返回成功跳转页面？能不能直接跳转到Controller
            }
            else{
                return "redirect:/tfPersonDetail"+"?articleId="+articleId;
            }
        }else {
            return "redirect:/tfPersonDetail"+"?articleId="+articleId;  //返回成功跳转页面？能不能直接跳转到Controller
        }
    }
}
